package main

import (
	"html/template"
	"net/http"
)

func f1(w http.ResponseWriter, r *http.Request) {
	//定义函数
	kua := func(name string) (string, error) {
		return name + "年轻帅气", nil
	}
	//定义模版
	//解析模版
	t := template.New("index.tmpl")
	// 告诉引擎多了一个自定义函数kua
	t.Funcs(template.FuncMap{
		"kua": kua,
	})
	_, err := t.ParseFiles("./index.tmpl")
	if err != nil {
		panic(err)
	}
	name := "张三"
	//渲染模版
	t.Execute(w, name)
}

func demo(w http.ResponseWriter, r *http.Request) {
	//被包含的写在后面
	t, err := template.ParseFiles("./index.tmpl", "./ul.tmpl")
	if err != nil {
		panic(err)
	}
	name := "李四"
	t.Execute(w, name)
}

func main() {
	http.HandleFunc("/", f1)
	http.HandleFunc("/tmpl", demo)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}
}
